{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 04 - Graphical Causal Models\n",
    "\n",
    "\n",
    "## Thinking About Causality\n",
    "\n",
    "Have you ever noticed how those cooks in YouTube videos are excellent at describing food? \"Reduce the sauce until it reaches a velvety consistency\". If you are just learning to cook, you have no idea what this even means. Just give me the time I should leave this thing on the stove! With causality, it's the same thing. Suppose you walk into a bar and hear folks discussing causality (probably a bar next to an economics department). In that case, you will hear them say how the confounding of income made it challenging to identify the immigration effect on that neighborhood, so they had to use an instrumental variable. And by now, you might not understand what they are talking about. But I'll fix at least some of this problem right now. \n",
    "\n",
    "Graphical models are the language of causality. They are not only what you use to talk with other brave and true causality aficionados but also something you use to make your own thoughts more transparent. \n",
    "\n",
    "As a starting point, let's take conditional independence of the potential outcomes, for example. This is one of the main assumptions that we require to be true when making causal inference:\n",
    "\n",
    "$\n",
    "(Y_0, Y_1) \\perp T | X\n",
    "$\n",
    "\n",
    "Conditional Independence makes it possible for us to measure an effect on the outcome solely due to the treatment and not any other variable lurking around. The classic example of this is the effect of a medicine on an ill patient. If only highly sick patients get the drug, it might even look like giving the medication decreases the patients' health. That is because the effect of the severity is getting mixed up with the drug's effect. If we break down the patients into severe and not severe cases and analyse the drug impact in each subgroup, we will get a clearer picture of the actual effect. This breaking down the population by its features is what we call controlling for or conditioning on X. By conditioning on the severe cases, the treatment mechanism becomes as good as random. Patients within the severe group may or may not receive the drug only due to chance, not due to a high severity anymore, since all patients are the same on this dimension. And if treatment is randomly assigned within groups, the treatment becomes conditionally independent of the potential outcomes. \n",
    "\n",
    "Independence and conditional independence are central to causal inference. Yet, it can be pretty challenging to wrap our heads around them. But this can change if we use the correct language to describe this problem. Here is where **causal graphical models** come in. A causal graphical model is a way to represent how causality works in terms of what causes what. \n",
    "\n",
    "A graphical model looks like this"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:30.677797Z",
     "start_time": "2023-08-01T11:06:28.800270Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import graphviz as gr\n",
    "from matplotlib import style\n",
    "import seaborn as sns\n",
    "from matplotlib import pyplot as plt\n",
    "style.use(\"fivethirtyeight\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:31.604638Z",
     "start_time": "2023-08-01T11:06:30.679996Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"292pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 291.95 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 287.95,-184 287.95,4 -4,4\"/>\n",
       "<!-- Z -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>Z</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- Z&#45;&gt;X -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>Z&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27,-143.7C27,-135.98 27,-126.71 27,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-118.1 27,-108.1 23.5,-118.1 30.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M84.43,-146.83C74.25,-136.94 60.48,-123.55 48.97,-112.36\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"51.41,-109.85 41.8,-105.38 46.53,-114.87 51.41,-109.85\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-143.7C99,-135.98 99,-126.71 99,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-118.1 99,-108.1 95.5,-118.1 102.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- medicine -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>medicine</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-90\" rx=\"43.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">medicine</text>\n",
       "</g>\n",
       "<!-- survived -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>survived</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"243\" cy=\"-18\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"243\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">survived</text>\n",
       "</g>\n",
       "<!-- medicine&#45;&gt;survived -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>medicine&#45;&gt;survived</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M215.71,-72.05C219.92,-63.89 225.05,-53.91 229.72,-44.82\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"232.9,-46.28 234.37,-35.79 226.68,-43.08 232.9,-46.28\"/>\n",
       "</g>\n",
       "<!-- severeness -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>severeness</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"226\" cy=\"-162\" rx=\"48.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"226\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">severeness</text>\n",
       "</g>\n",
       "<!-- severeness&#45;&gt;medicine -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>severeness&#45;&gt;medicine</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M221.4,-144.05C219.29,-136.26 216.72,-126.82 214.35,-118.08\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"217.69,-117.01 211.69,-108.28 210.93,-118.84 217.69,-117.01\"/>\n",
       "</g>\n",
       "<!-- severeness&#45;&gt;survived -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>severeness&#45;&gt;survived</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M240.05,-144.57C247.56,-134.68 256.06,-121.43 260,-108 266.06,-87.34 260.78,-63.32 254.55,-45.45\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"257.77,-44.07 250.96,-35.96 251.22,-46.55 257.77,-44.07\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a9a65c50>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"Z\", \"X\")\n",
    "g.edge(\"U\", \"X\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "\n",
    "g.edge(\"medicine\", \"survived\")\n",
    "g.edge(\"severeness\", \"survived\")\n",
    "g.edge(\"severeness\", \"medicine\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each node is a random variable. We use arrows, or edges, to show if a variable causes another. In the first graphical model above, we are saying that Z causes X and that U causes X and Y. To give a more concrete example, we can translate our thoughts about the impact of the medicine on patient survival in the second graph above. Severeness causes both medicine and survival, and medicine also causes survival. As we will see, these causal graphical models language will help us make our thinking about causality clearer, as it clarifies our beliefs about how the world works. \n",
    "\n",
    "## Crash Course in Graphical Models\n",
    "\n",
    "There are [whole semesters on graphical models](https://www.coursera.org/specializations/probabilistic-graphical-models). But, for our purpose, it is just (very) important that we understand what kind of independence and conditional independence assumptions a graphical model entails. As we shall see, independence flows through a graphical model as water flows through a stream. We can stop this flow or enable it, depending on how we treat the variables in it. To understand this, let's examine some common graphical structures and examples. They will be pretty straightforward, but they are sufficient building blocks to understand everything about independence and conditional independence on graphical models.\n",
    "\n",
    "First, look at this very simple graph. A causes B, B causes C. Or X causes Y which causes Z."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:31.768306Z",
     "start_time": "2023-08-01T11:06:31.606937Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"298pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 298.44 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 294.44,-184 294.44,4 -4,4\"/>\n",
       "<!-- A -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>A</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n",
       "</g>\n",
       "<!-- B -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>B</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n",
       "</g>\n",
       "<!-- A&#45;&gt;B -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>A&#45;&gt;B</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27,-143.7C27,-135.98 27,-126.71 27,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-118.1 27,-108.1 23.5,-118.1 30.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- C -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>C</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n",
       "</g>\n",
       "<!-- B&#45;&gt;C -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>B&#45;&gt;C</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27,-71.7C27,-63.98 27,-54.71 27,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-46.1 27,-36.1 23.5,-46.1 30.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"red\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>X&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-143.7C99,-135.98 99,-126.71 99,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-118.1 99,-108.1 95.5,-118.1 102.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- Z -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Z</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z</text>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;Z -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>Y&#45;&gt;Z</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-71.7C99,-63.98 99,-54.71 99,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-46.1 99,-36.1 95.5,-46.1 102.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- causal knowledge -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>causal knowledge</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"217\" cy=\"-162\" rx=\"73.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"217\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">causal knowledge</text>\n",
       "</g>\n",
       "<!-- solve problems -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>solve problems</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"217\" cy=\"-90\" rx=\"64.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"217\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">solve problems</text>\n",
       "</g>\n",
       "<!-- causal knowledge&#45;&gt;solve problems -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>causal knowledge&#45;&gt;solve problems</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M217,-143.7C217,-135.98 217,-126.71 217,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"220.5,-118.1 217,-108.1 213.5,-118.1 220.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- job promotion -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>job promotion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"217\" cy=\"-18\" rx=\"61.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"217\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">job promotion</text>\n",
       "</g>\n",
       "<!-- solve problems&#45;&gt;job promotion -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>solve problems&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M217,-71.7C217,-63.98 217,-54.71 217,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"220.5,-46.1 217,-36.1 213.5,-46.1 220.5,-46.1\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a833ced0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"A\", \"B\")\n",
    "g.edge(\"B\", \"C\")\n",
    "\n",
    "g.edge(\"X\", \"Y\")\n",
    "g.edge(\"Y\", \"Z\")\n",
    "g.node(\"Y\", \"Y\", color=\"red\")\n",
    "\n",
    "\n",
    "g.edge(\"causal knowledge\", \"solve problems\")\n",
    "g.edge(\"solve problems\", \"job promotion\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the first graph, dependence flows in the direction of the arrows. Notice that dependence is symmetric, although it is a little less intuitive. To give a more concrete example, let's say that knowing about causal inference is the only way to solve business problems, and solving those problems is the only way to get a job promotion. So causal knowledge implies solving issues that cause a job promotion. We can say here that job promotion is dependent on causal knowledge. The greater the causal expertise, the greater your chances of getting a promotion. Also, the greater your chances of promotion, the greater your chance of having causal knowledge. Otherwise, it would be difficult to get a promotion. \n",
    "\n",
    "Now, let's say I condition on the intermediary variable. In this case, the dependence is blocked. So, X and Z are independent given Y. In the graph above, red indicates that Y is a conditioned variable. By the same token, in our example, if I know that you are good at solving problems, knowing that you know causal inference doesn't give any further information about your chances of getting a job promotion. In mathematical terms, $E[Promotion|Solve \\ problems, Causal \\ knowledge]=E[Promotion|Solve \\ problems]$. The inverse is also true; once I know how good you are at solving problems, knowing about your job promotion status gives me no further information about how likely you are to know causal inference. \n",
    "\n",
    "As a general rule, the dependence flow in the direct path from A to C is blocked when we condition on an intermediary variable B. Or,\n",
    "\n",
    "$A \\not \\perp C$\n",
    "\n",
    "and\n",
    "\n",
    "$\n",
    "A \\perp C | B\n",
    "$\n",
    "\n",
    "Now, let's consider a fork structure. The same variable causes two other variables down the graph. In this case, the dependence flows backward through the arrows, and we have a **backdoor path**. We can close the backdoor path and shut down dependence by conditioning on the common cause."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:31.971823Z",
     "start_time": "2023-08-01T11:06:31.770741Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"594pt\" height=\"116pt\"\n",
       " viewBox=\"0.00 0.00 594.14 116.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-112 590.14,-112 590.14,4 -4,4\"/>\n",
       "<!-- C -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>C</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"63\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n",
       "</g>\n",
       "<!-- A -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>A</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n",
       "</g>\n",
       "<!-- C&#45;&gt;A -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>C&#45;&gt;A</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.65,-72.76C50.29,-64.28 44.85,-53.71 39.96,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"42.99,-42.44 35.3,-35.15 36.77,-45.64 42.99,-42.44\"/>\n",
       "</g>\n",
       "<!-- B -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>B</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n",
       "</g>\n",
       "<!-- C&#45;&gt;B -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>C&#45;&gt;B</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M71.35,-72.76C75.71,-64.28 81.15,-53.71 86.04,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"89.23,-45.64 90.7,-35.15 83.01,-42.44 89.23,-45.64\"/>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"red\" cx=\"207\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"171\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>X&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M198.65,-72.76C194.29,-64.28 188.85,-53.71 183.96,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"186.99,-42.44 179.3,-35.15 180.77,-45.64 186.99,-42.44\"/>\n",
       "</g>\n",
       "<!-- Z -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Z</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"243\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"243\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Z -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>X&#45;&gt;Z</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M215.35,-72.76C219.71,-64.28 225.15,-53.71 230.04,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"233.23,-45.64 234.7,-35.15 227.01,-42.44 233.23,-45.64\"/>\n",
       "</g>\n",
       "<!-- statistics -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>statistics</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"435\" cy=\"-90\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"435\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">statistics</text>\n",
       "</g>\n",
       "<!-- causal inference -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>causal inference</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"356\" cy=\"-18\" rx=\"67.69\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"356\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">causal inference</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;causal inference -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;causal inference</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M417.47,-73.46C407.13,-64.3 393.84,-52.53 382.27,-42.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"384.39,-39.48 374.58,-35.47 379.75,-44.72 384.39,-39.48\"/>\n",
       "</g>\n",
       "<!-- machine learning -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>machine learning</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"514\" cy=\"-18\" rx=\"72.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"514\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">machine learning</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;machine learning -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;machine learning</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M452.53,-73.46C462.87,-64.3 476.16,-52.53 487.73,-42.27\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"490.25,-44.72 495.42,-35.47 485.61,-39.48 490.25,-44.72\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a830b210>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"C\", \"A\")\n",
    "g.edge(\"C\", \"B\")\n",
    "\n",
    "g.edge(\"X\", \"Y\")\n",
    "g.edge(\"X\", \"Z\")\n",
    "g.node(\"X\", \"X\", color=\"red\")\n",
    "\n",
    "g.edge(\"statistics\", \"causal inference\")\n",
    "g.edge(\"statistics\", \"machine learning\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, let's say your knowledge of statistics causes you to know more about causal inference and machine learning. If I don't know your level of statistical knowledge, then knowing that you are good at causal inference makes it more likely that you are also good at machine learning. That is because even if I don't know your level of statistical knowledge, I can infer it from your causal inference knowledge. If you are good at causal inference, you are probably good at statistics, making it more likely that you are good at machine learning. \n",
    "\n",
    "Now, if I condition on your knowledge about statistics, then how much you know about machine learning becomes independent of how much you know about causal inference. Knowing your level of statistics already gives me all the information I need to infer the level of your machine learning skills. You see, knowing your level of causal inference will provide no further information in this case. \n",
    "\n",
    "As a general rule, two variables that share a common cause are dependent, but independent when we condition on the common cause. Or\n",
    "\n",
    "$A \\not \\perp  B$\n",
    "\n",
    "and\n",
    "\n",
    "$\n",
    "A \\perp B | C\n",
    "$\n",
    "\n",
    "The only structure that is missing is the collider. A collider is when two arrows collide on a single variable. We can say that in this case, both variables share a common effect. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:32.177819Z",
     "start_time": "2023-08-01T11:06:31.974146Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"460pt\" height=\"116pt\"\n",
       " viewBox=\"0.00 0.00 459.85 116.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-112 455.85,-112 455.85,4 -4,4\"/>\n",
       "<!-- B -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>B</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n",
       "</g>\n",
       "<!-- C -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>C</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"63\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n",
       "</g>\n",
       "<!-- B&#45;&gt;C -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>B&#45;&gt;C</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M35.35,-72.76C39.71,-64.28 45.15,-53.71 50.04,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"53.23,-45.64 54.7,-35.15 47.01,-42.44 53.23,-45.64\"/>\n",
       "</g>\n",
       "<!-- A -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>A</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n",
       "</g>\n",
       "<!-- A&#45;&gt;C -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>A&#45;&gt;C</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M90.65,-72.76C86.29,-64.28 80.85,-53.71 75.96,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"78.99,-42.44 71.3,-35.15 72.77,-45.64 78.99,-42.44\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"171\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"red\" cx=\"196\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"196\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;X -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>Y&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M176.92,-72.41C179.78,-64.42 183.27,-54.64 186.48,-45.64\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"189.88,-46.55 189.94,-35.96 183.28,-44.2 189.88,-46.55\"/>\n",
       "</g>\n",
       "<!-- Z -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Z</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"243\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"243\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z</text>\n",
       "</g>\n",
       "<!-- Z&#45;&gt;X -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>Z&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M232.33,-73.12C226.45,-64.34 219.01,-53.26 212.39,-43.42\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"215.1,-41.17 206.62,-34.82 209.29,-45.07 215.1,-41.17\"/>\n",
       "</g>\n",
       "<!-- statistics -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>statistics</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"329\" cy=\"-90\" rx=\"40.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"329\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">statistics</text>\n",
       "</g>\n",
       "<!-- job promotion -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>job promotion</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"374\" cy=\"-18\" rx=\"61.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"374\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">job promotion</text>\n",
       "</g>\n",
       "<!-- statistics&#45;&gt;job promotion -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>statistics&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M339.66,-72.41C345.02,-64.08 351.63,-53.8 357.61,-44.49\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"360.64,-46.26 363.1,-35.96 354.75,-42.48 360.64,-46.26\"/>\n",
       "</g>\n",
       "<!-- flatter -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>flatter</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"420\" cy=\"-90\" rx=\"31.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"420\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">flatter</text>\n",
       "</g>\n",
       "<!-- flatter&#45;&gt;job promotion -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>flatter&#45;&gt;job promotion</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M409.33,-72.76C403.86,-64.44 397.07,-54.1 390.91,-44.73\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"393.67,-42.56 385.25,-36.12 387.82,-46.4 393.67,-42.56\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3cacfcf10>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"B\", \"C\")\n",
    "g.edge(\"A\", \"C\")\n",
    "\n",
    "g.edge(\"Y\", \"X\")\n",
    "g.edge(\"Z\", \"X\")\n",
    "g.node(\"X\", \"X\", color=\"red\")\n",
    "\n",
    "g.edge(\"statistics\", \"job promotion\")\n",
    "g.edge(\"flatter\", \"job promotion\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, consider that there are two ways to get a job promotion. You can either be good at statistics or flatter your boss. If I don't condition on your job promotion, that is, I know nothing if you will or won't get it, then your level of statistics and flattering are independent. In other words, knowing how good you are at statistics tells me nothing about how good you are at flattering your boss. On the other hand, if you did get a job promotion, suddenly, knowing your level of statistics tells me about your flattering level. If you are bad at statistics and did get a promotion, you will likely know how to flatter. Otherwise, you wouldn't get a promotion. Conversely, if you are bad at flattering, it must be the case that you are good at statistics. This phenomenon is sometimes called **explaining away**, because one cause already explains the effect, making the other cause less likely.\n",
    "\n",
    "As a general rule, conditioning on a collider opens the dependence path. Not conditioning on it leaves it closed. Or\n",
    "\n",
    "$A \\perp B$\n",
    "\n",
    "and\n",
    "\n",
    "$\n",
    "A \\not \\perp B | C\n",
    "$\n",
    "\n",
    "Knowing the three structures, we can derive an even more general rule. A path is blocked if and only if:\n",
    "1. It contains a non collider that has been conditioned on\n",
    "2. It contains a collider that has not been conditioned on and has no descendants that have been conditioned on.\n",
    "\n",
    "Here is a cheat sheet about how dependence flows in a graph. I've taken from a [Stanford presentation](http://ai.stanford.edu/~paskin/gm-short-course/lec2.pdf) by Mark Paskin.  The arrows with lines at their tips signify independence, and the arrows without lines at their tips signify dependence.\n",
    "\n",
    "![img](data/img/graph-flow.png)\n",
    "\n",
    "As a final example, try to figure out some independence and dependence relationship in the following causal graph.\n",
    "1. Is $D \\perp C$?\n",
    "2. Is $D \\perp C| A $ ?\n",
    "3. Is $D \\perp C| G $ ?\n",
    "4. Is $A \\perp F $ ?\n",
    "5. Is $A \\perp F|E $ ?\n",
    "6. Is $A \\perp F|E,C $ ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:32.379802Z",
     "start_time": "2023-08-01T11:06:32.179448Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"206pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 206.00 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 202,-184 202,4 -4,4\"/>\n",
       "<!-- C -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>C</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n",
       "</g>\n",
       "<!-- A -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>A</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n",
       "</g>\n",
       "<!-- C&#45;&gt;A -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>C&#45;&gt;A</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M84.43,-146.83C74.25,-136.94 60.48,-123.55 48.97,-112.36\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"51.41,-109.85 41.8,-105.38 46.53,-114.87 51.41,-109.85\"/>\n",
       "</g>\n",
       "<!-- B -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>B</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n",
       "</g>\n",
       "<!-- C&#45;&gt;B -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>C&#45;&gt;B</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-143.7C99,-135.98 99,-126.71 99,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-118.1 99,-108.1 95.5,-118.1 102.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- G -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>G</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">G</text>\n",
       "</g>\n",
       "<!-- A&#45;&gt;G -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>A&#45;&gt;G</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27,-71.7C27,-63.98 27,-54.71 27,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-46.1 27,-36.1 23.5,-46.1 30.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- E -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>E</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"135\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"135\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">E</text>\n",
       "</g>\n",
       "<!-- B&#45;&gt;E -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>B&#45;&gt;E</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M107.35,-72.76C111.71,-64.28 117.15,-53.71 122.04,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"125.23,-45.64 126.7,-35.15 119.01,-42.44 125.23,-45.64\"/>\n",
       "</g>\n",
       "<!-- D -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>D</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">D</text>\n",
       "</g>\n",
       "<!-- D&#45;&gt;A -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>D&#45;&gt;A</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27,-143.7C27,-135.98 27,-126.71 27,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-118.1 27,-108.1 23.5,-118.1 30.5,-118.1\"/>\n",
       "</g>\n",
       "<!-- F -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>F</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"171\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">F</text>\n",
       "</g>\n",
       "<!-- F&#45;&gt;E -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>F&#45;&gt;E</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M162.65,-72.76C158.29,-64.28 152.85,-53.71 147.96,-44.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"150.99,-42.44 143.3,-35.15 144.77,-45.64 150.99,-42.44\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe36808fd10>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"C\", \"A\")\n",
    "g.edge(\"C\", \"B\")\n",
    "g.edge(\"D\", \"A\")\n",
    "g.edge(\"B\", \"E\")\n",
    "g.edge(\"F\", \"E\")\n",
    "g.edge(\"A\", \"G\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Answers**:\n",
    "1. $D \\perp C$. It contains a collider that it has **not** been conditioned on.\n",
    "2. $D \\not\\perp C| A $. It contains a collider that it has  been conditioned on.\n",
    "3. $D \\not\\perp C| G $. It contains the descendent of a collider that has  been conditioned on. You can see G as some kind of proxy for A here.\n",
    "4. $A \\perp F $. It contains a collider, B->E<-F, that it has **not** been conditioned on.\n",
    "5. $A \\not\\perp F|E $. It contains a collider, B->E<-F, that it has been conditioned on.\n",
    "6. $A \\perp F|E, C $. It contains a collider, B->E<-F, that it has been conditioned on, but it contains a non collider that has been conditioned on. Conditioning on E opens the path, but conditioning on C closes it again.\n",
    "\n",
    "Knowing about causal graphical models enables us to understand the problems that arise in causal inference. As we've seen, the problem always boils down to bias. \n",
    "\n",
    "$\n",
    "E[Y|T=1] - E[Y|T=0] = \\underbrace{E[Y_1 - Y_0|T=1]}_{ATET} + \\underbrace{\\{ E[Y_0|T=1] - E[Y_0|T=0] \\}}_{BIAS}\n",
    "$\n",
    "\n",
    "Graphical models allow us to diagnose which bias we are dealing with and what are the tools we need to correct for them.\n",
    "\n",
    "## Confounding Bias\n",
    "\n",
    "![img](./data/img/causal-graph/both_crap.png)\n",
    "\n",
    "The first significant cause of bias is confounding. It happens when the treatment and the outcome share a common cause. For example, let's say that the treatment is education, and the outcome is income. It is hard to know the causal effect of education on wages because both share a common cause: intelligence. So we could argue that more educated people earn more money simply because they are more intelligent, not because they have more education. We need to close all backdoor paths between the treatment and the outcome to identify the causal effect. If we do so, the only effect that will be left is the direct effect T->Y. In our example, if we control for intelligence, that is, we compare people with the same level of intellect but different levels of education, the difference in the outcome will be only due to the difference in schooling since intelligence will be the same for everyone. To fix confounding bias, we need to control all common causes of the treatment and the outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:32.564754Z",
     "start_time": "2023-08-01T11:06:32.381563Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"227pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 227.00 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 223,-184 223,4 -4,4\"/>\n",
       "<!-- X -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"63\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;T -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M54.65,-144.76C50.29,-136.28 44.85,-125.71 39.96,-116.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"42.99,-114.44 35.3,-107.15 36.77,-117.64 42.99,-114.44\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"66\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"66\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>X&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M63.36,-143.87C63.87,-119.67 64.81,-75.21 65.42,-46.39\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"68.92,-46.26 65.64,-36.19 61.93,-46.11 68.92,-46.26\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M36.05,-72.76C40.82,-64.19 46.78,-53.49 52.13,-43.9\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"55.19,-45.59 57,-35.15 49.08,-42.18 55.19,-45.59\"/>\n",
       "</g>\n",
       "<!-- Intelligence -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Intelligence</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"167\" cy=\"-162\" rx=\"51.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"167\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Intelligence</text>\n",
       "</g>\n",
       "<!-- Educ -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"148\" cy=\"-90\" rx=\"28.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"148\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Educ</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;Educ -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;Educ</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M162.4,-144.05C160.25,-136.14 157.65,-126.54 155.24,-117.69\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"158.55,-116.52 152.56,-107.79 151.8,-118.36 158.55,-116.52\"/>\n",
       "</g>\n",
       "<!-- Wage -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Wage</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"164\" cy=\"-18\" rx=\"31.4\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"164\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Wage</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;Wage -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M174.89,-143.75C179.11,-133.58 183.86,-120.33 186,-108 188.74,-92.24 189.13,-87.69 186,-72 184.18,-62.87 180.76,-53.35 177.15,-44.93\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"180.24,-43.29 172.9,-35.65 173.88,-46.2 180.24,-43.29\"/>\n",
       "</g>\n",
       "<!-- Educ&#45;&gt;Wage -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>Educ&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M151.87,-72.05C153.65,-64.26 155.81,-54.82 157.81,-46.08\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"161.24,-46.8 160.05,-36.28 154.41,-45.24 161.24,-46.8\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a9f57690>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"X\", \"T\")\n",
    "g.edge(\"X\", \"Y\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "\n",
    "g.edge(\"Intelligence\", \"Educ\"),\n",
    "g.edge(\"Intelligence\", \"Wage\"),\n",
    "g.edge(\"Educ\", \"Wage\")\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unfortunately, it is not always possible to control all common causes. Sometimes, there are unknown causes or known causes that we can't measure. The case of intelligence is one of the latter. Despite all the effort, scientists haven't yet figured out how to measure intelligence well. I'll use U to denote unmeasured variables here. Now, assume for a moment that intelligence can't affect your education directly. It affects how well you do on the SATs, but the SATs determine your level of schooling since it opens the possibility of a good college. Even if we can't control for the unmeasurable intelligence, we can control for SAT and close that backdoor path."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:32.741197Z",
     "start_time": "2023-08-01T11:06:32.566586Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"446pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 446.00 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-256 442,-256 442,4 -4,4\"/>\n",
       "<!-- X1 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">X1</text>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"95\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"95\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X1&#45;&gt;T -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X1&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M41.09,-146.5C50.57,-136.73 63.24,-123.69 73.92,-112.7\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"76.47,-115.1 80.93,-105.49 71.45,-110.22 76.47,-115.1\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"95\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"95\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- X1&#45;&gt;Y -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>X1&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M31.69,-144.08C37.09,-125.77 46.78,-96 59,-72 64.07,-62.04 70.71,-51.73 76.9,-42.88\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"79.87,-44.75 82.87,-34.59 74.19,-40.66 79.87,-44.75\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M95,-71.7C95,-63.98 95,-54.71 95,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"98.5,-46.1 95,-36.1 91.5,-46.1 98.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- X2 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>X2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">X2</text>\n",
       "</g>\n",
       "<!-- X2&#45;&gt;T -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>X2&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M98.01,-143.7C97.57,-135.98 97.04,-126.71 96.55,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"100.04,-117.89 95.98,-108.1 93.05,-118.29 100.04,-117.89\"/>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"127\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"127\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M131.16,-215.98C137.68,-185.96 147.93,-122.06 131,-72 127.42,-61.41 121.1,-50.97 114.75,-42.19\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"117.35,-39.82 108.48,-34.02 111.79,-44.08 117.35,-39.82\"/>\n",
       "</g>\n",
       "<!-- U&#45;&gt;X2 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>U&#45;&gt;X2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M120.36,-216.41C117.09,-208.22 113.06,-198.14 109.38,-188.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"112.55,-187.45 105.59,-179.47 106.05,-190.05 112.55,-187.45\"/>\n",
       "</g>\n",
       "<!-- Family Income -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Family Income</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"248\" cy=\"-162\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"248\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Family Income</text>\n",
       "</g>\n",
       "<!-- Educ -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>Educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"344\" cy=\"-90\" rx=\"28.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"344\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Educ</text>\n",
       "</g>\n",
       "<!-- Family Income&#45;&gt;Educ -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>Family Income&#45;&gt;Educ</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M270.27,-144.76C284.43,-134.43 302.85,-121.01 317.73,-110.15\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"320.01,-112.82 326.03,-104.1 315.89,-107.17 320.01,-112.82\"/>\n",
       "</g>\n",
       "<!-- Wage -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>Wage</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"344\" cy=\"-18\" rx=\"31.4\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"344\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Wage</text>\n",
       "</g>\n",
       "<!-- Family Income&#45;&gt;Wage -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>Family Income&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M258.68,-144.12C270.16,-126.11 289.03,-96.81 306,-72 312.49,-62.51 319.81,-52.21 326.28,-43.23\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"329.14,-45.26 332.17,-35.11 323.47,-41.15 329.14,-45.26\"/>\n",
       "</g>\n",
       "<!-- Educ&#45;&gt;Wage -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>Educ&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M344,-71.7C344,-63.98 344,-54.71 344,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"347.5,-46.1 344,-36.1 340.5,-46.1 347.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- SAT -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>SAT</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"359\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"359\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">SAT</text>\n",
       "</g>\n",
       "<!-- SAT&#45;&gt;Educ -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>SAT&#45;&gt;Educ</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M355.37,-144.05C353.7,-136.26 351.68,-126.82 349.8,-118.08\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"353.22,-117.32 347.7,-108.28 346.37,-118.79 353.22,-117.32\"/>\n",
       "</g>\n",
       "<!-- Intelligence -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>Intelligence</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"386\" cy=\"-234\" rx=\"51.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"386\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">Intelligence</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;Wage -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M389.65,-215.91C391.68,-205.57 393.98,-192.09 395,-180 399.06,-131.82 400.84,-116.52 382,-72 377.59,-61.57 370.82,-51.2 364.21,-42.43\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"366.95,-40.25 358.01,-34.57 361.46,-44.59 366.95,-40.25\"/>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;SAT -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;SAT</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M379.46,-216.05C376.38,-208.06 372.63,-198.33 369.18,-189.4\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"372.34,-187.86 365.48,-179.79 365.81,-190.38 372.34,-187.86\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a9f63ad0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"X1\", \"T\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"X2\", \"T\")\n",
    "g.edge(\"X1\", \"Y\")\n",
    "g.edge(\"U\", \"X2\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "\n",
    "g.edge(\"Family Income\", \"Educ\")\n",
    "g.edge(\"Educ\", \"Wage\")\n",
    "g.edge(\"SAT\", \"Educ\")\n",
    "g.edge(\"Family Income\", \"Wage\")\n",
    "g.edge(\"Intelligence\", \"SAT\")\n",
    "g.edge(\"Intelligence\", \"Wage\")\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following graph, conditioning on X1 and X2, or SAT and family income, is sufficient to close all backdoor paths between the treatment and the outcome. In other words, $(Y_0, Y_1) \\perp T | X1, X2$. So even if we can't measure all common causes, we can still attain conditional independence if we control for measurable variables that mediate the effect of the unmeasured on the treatment. One quick note, we also have $(Y_0, Y_1) \\perp T | X1, U$, but since we can't observe U, we can't condition on it. \n",
    "\n",
    "But what if that is not the case? What if the unmeasured variable causes the treatment and the outcome directly? In the following example, intelligence causes education and income now. So there is confounding in the relationship between the treatment education and the outcome wage. In this case, we can't control the confounder because it is unmeasurable. However, we have other measured variables that can act as a proxy for the confounder. Those variables are not in the backdoor path, but controlling for them will help lower the bias (but it won't eliminate it). Those variables are sometimes referred to as surrogate confounders.\n",
    "\n",
    "In our example, we can't measure intelligence, but we can measure some of its causes, like the father's and mother's education, and some of its effects, like IQ or SAT score. Controlling for those surrogate variables is not sufficient to eliminate bias, but it helps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:32.914262Z",
     "start_time": "2023-08-01T11:06:32.742990Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"411pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 411.39 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-256 407.39,-256 407.39,4 -4,4\"/>\n",
       "<!-- X -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- U -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>U</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">U</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;U -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>X&#45;&gt;U</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M55,-215.7C55,-207.98 55,-198.71 55,-190.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"58.5,-190.1 55,-180.1 51.5,-190.1 58.5,-190.1\"/>\n",
       "</g>\n",
       "<!-- T -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;T -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>U&#45;&gt;T</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M48.36,-144.41C45.09,-136.22 41.06,-126.14 37.38,-116.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"40.55,-115.45 33.59,-107.47 34.05,-118.05 40.55,-115.45\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- U&#45;&gt;Y -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>U&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M58.25,-143.89C60.05,-133.54 62.09,-120.06 63,-108 64.2,-92.04 64.2,-87.96 63,-72 62.36,-63.52 61.16,-54.34 59.88,-46.04\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.32,-45.41 58.25,-36.11 56.42,-46.55 63.32,-45.41\"/>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M33.64,-72.41C36.91,-64.22 40.94,-54.14 44.62,-44.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"47.95,-46.05 48.41,-35.47 41.45,-43.45 47.95,-46.05\"/>\n",
       "</g>\n",
       "<!-- Intelligence -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Intelligence</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"246\" cy=\"-162\" rx=\"51.99\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"246\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Intelligence</text>\n",
       "</g>\n",
       "<!-- IQ -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>IQ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"137\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"137\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">IQ</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;IQ -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;IQ</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M222.35,-145.81C205.39,-134.92 182.5,-120.22 164.69,-108.78\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"166.2,-105.6 155.9,-103.14 162.42,-111.49 166.2,-105.6\"/>\n",
       "</g>\n",
       "<!-- SAT -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>SAT</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"209\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"209\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">SAT</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;SAT -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;SAT</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M237.04,-144.05C232.66,-135.77 227.3,-125.62 222.44,-116.42\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"225.39,-114.51 217.62,-107.31 219.2,-117.78 225.39,-114.51\"/>\n",
       "</g>\n",
       "<!-- Educ -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>Educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"283\" cy=\"-90\" rx=\"28.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"283\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Educ</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;Educ -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;Educ</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M254.96,-144.05C259.34,-135.77 264.7,-125.62 269.56,-116.42\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"272.8,-117.78 274.38,-107.31 266.61,-114.51 272.8,-117.78\"/>\n",
       "</g>\n",
       "<!-- Wage -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>Wage</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"311\" cy=\"-18\" rx=\"31.4\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"311\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Wage</text>\n",
       "</g>\n",
       "<!-- Intelligence&#45;&gt;Wage -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>Intelligence&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M277.62,-147.47C293.74,-138.75 311.84,-125.7 321,-108 331.01,-88.66 327.08,-63.84 321.46,-45.33\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"324.75,-44.13 318.24,-35.77 318.11,-46.36 324.75,-44.13\"/>\n",
       "</g>\n",
       "<!-- Father&#39;s Educ -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>Father&#39;s Educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"202\" cy=\"-234\" rx=\"58.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"202\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">Father&#39;s Educ</text>\n",
       "</g>\n",
       "<!-- Father&#39;s Educ&#45;&gt;Intelligence -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>Father&#39;s Educ&#45;&gt;Intelligence</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M212.65,-216.05C217.84,-207.8 224.19,-197.7 229.95,-188.54\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"233.09,-190.12 235.45,-179.79 227.16,-186.39 233.09,-190.12\"/>\n",
       "</g>\n",
       "<!-- Mother&#39;s Educ -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>Mother&#39;s Educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"341\" cy=\"-234\" rx=\"62.29\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"341\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">Mother&#39;s Educ</text>\n",
       "</g>\n",
       "<!-- Mother&#39;s Educ&#45;&gt;Intelligence -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>Mother&#39;s Educ&#45;&gt;Intelligence</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M318.97,-216.76C306.08,-207.27 289.63,-195.15 275.62,-184.82\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"277.39,-181.78 267.26,-178.67 273.24,-187.42 277.39,-181.78\"/>\n",
       "</g>\n",
       "<!-- Educ&#45;&gt;Wage -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>Educ&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M289.64,-72.41C292.87,-64.34 296.83,-54.43 300.46,-45.35\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"303.75,-46.54 304.22,-35.96 297.25,-43.94 303.75,-46.54\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a9f60350>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"X\", \"U\")\n",
    "g.edge(\"U\", \"T\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"U\", \"Y\")\n",
    "\n",
    "g.edge(\"Intelligence\", \"IQ\")\n",
    "g.edge(\"Intelligence\", \"SAT\")\n",
    "g.edge(\"Father's Educ\", \"Intelligence\")\n",
    "g.edge(\"Mother's Educ\", \"Intelligence\")\n",
    "\n",
    "g.edge(\"Intelligence\", \"Educ\")\n",
    "g.edge(\"Educ\", \"Wage\")\n",
    "g.edge(\"Intelligence\", \"Wage\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selection Bias\n",
    "\n",
    "You might think it is a good idea to add everything you can measure to your model just to ensure you don't have confounding bias. Well, think again.\n",
    "\n",
    "![image.png](./data/img/causal-graph/selection_bias.png)\n",
    "\n",
    "The second significant source of bias is what we will call selection bias. Here, I think the distinction between it and confounding bias is constructive, so I'll stick to it. If confounding bias happens when we don't control for a common cause, selection bias is more related to effects. One word of caution here, economists tend to refer to all sorts of biases as selection bias. \n",
    "\n",
    "Often, selection bias arises when we control for more variables than we should. It might be the case that the treatment and the potential outcome are marginally independent but become dependent once we condition on a collider. \n",
    "\n",
    "Imagine that with the help of some miracle, you can finally randomize education to measure its effect on wages. But just to be sure you won't have confounding, you control for many variables. Among them, you control for investments. But investment is not a common cause of education and wage. Instead, it is a consequence of both. More educated people both earn more and invest more. Also, those who make more invest more. Since investment is a collider, by conditioning on it, you are opening a second path between the treatment and the outcome, which will make it harder to measure the direct effect. One way to think about this is that by controlling investments, you look at small groups of the population where investment is the same and then find the effect of education on those groups. But by doing so, you are also indirectly and inadvertently not allowing wages to change much. As a result, you won't be able to see how education changes wages because you are not allowing wages to change as they should. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-08-01T11:06:33.165205Z",
     "start_time": "2023-08-01T11:06:32.947514Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"218pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 218.20 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 214.2,-184 214.2,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"37\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"37\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"red\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;X -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M26.76,-145.02C20.88,-134.86 14.06,-121.18 11,-108 6.13,-87.03 11.07,-63 16.7,-45.2\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"20.09,-46.1 20.03,-35.5 13.47,-43.83 20.09,-46.1\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"47\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"47\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M39.42,-144.05C40.52,-136.35 41.85,-127.03 43.09,-118.36\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"46.58,-118.67 44.53,-108.28 39.65,-117.68 46.58,-118.67\"/>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;X -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>Y&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M42.16,-72.05C39.9,-64.14 37.15,-54.54 34.63,-45.69\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"37.91,-44.44 31.8,-35.79 31.18,-46.37 37.91,-44.44\"/>\n",
       "</g>\n",
       "<!-- Educ -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>Educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"129\" cy=\"-162\" rx=\"28.7\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"129\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Educ</text>\n",
       "</g>\n",
       "<!-- Investments -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>Investments</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"140\" cy=\"-18\" rx=\"53.89\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"140\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Investments</text>\n",
       "</g>\n",
       "<!-- Educ&#45;&gt;Investments -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>Educ&#45;&gt;Investments</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M130.33,-143.87C132.2,-119.67 135.65,-75.21 137.88,-46.39\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"141.39,-46.43 138.67,-36.19 134.41,-45.89 141.39,-46.43\"/>\n",
       "</g>\n",
       "<!-- Wage -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>Wage</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"179\" cy=\"-90\" rx=\"31.4\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"179\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">Wage</text>\n",
       "</g>\n",
       "<!-- Educ&#45;&gt;Wage -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>Educ&#45;&gt;Wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M140.35,-145.12C146.67,-136.26 154.69,-125.04 161.77,-115.12\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"164.73,-116.99 167.7,-106.82 159.04,-112.92 164.73,-116.99\"/>\n",
       "</g>\n",
       "<!-- Wage&#45;&gt;Investments -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>Wage&#45;&gt;Investments</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M169.95,-72.76C165.36,-64.53 159.68,-54.32 154.49,-45.02\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"157.47,-43.16 149.54,-36.12 151.35,-46.56 157.47,-43.16\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe3a9f71fd0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"T\", \"X\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"Y\", \"X\")\n",
    "g.node(\"X\", \"X\", color=\"red\")\n",
    "\n",
    "g.edge(\"Educ\", \"Investments\")\n",
    "g.edge(\"Educ\", \"Wage\")\n",
    "g.edge(\"Wage\", \"Investments\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Imagine that investments and education take only 2 values to demonstrate why this is the case. Whether people invest or not. They are either educated or not. Initially, when we don't control for investments, the bias term is zero: $E[Y_0|T=1] - E[Y_0|T=0] = 0$ because the education was randomised. This means that the wage people would have if they didn't receive education $Wage_0$ is the same if they do or don't receive the education treatment. But what happens if we condition on investments?\n",
    "\n",
    "Looking at those who invest, we probably have the case that $E[Y_0|T=0, I=1] > E[Y_0|T=1, I=1]$. In words, among those who invest, the ones who manage to do so even without education are more likely to achieve high earnings, regardless of their education level. For this reason, the wage those people have, $Wage_0|T=0$, is probably higher than the wage the educated group would have if they didn't have education, $Wage_0|T=1$. A similar reasoning can be applied to those who don't invest, where we also probably have $E[Y_0|T=0, I=0] > E[Y_0|T=1, I=0]$. Those who don't invest even with education probably would have a lower wage, had they not got the education, than those who didn't invest but didn't have an education. \n",
    "\n",
    "To use a purely graphical argument, if someone invests, knowing that they have high education explains away the second cause, which is wage. Conditioned on investing, higher education is associated with low wages and we have a negative bias $E[Y_0|T=0, I=i] > E[Y_0|T=1, I=i]$. \n",
    "\n",
    "As a side note, all of this we've discussed is true if we condition on any descendent of a common effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.49.1 (20211004.0028)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"90pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 90.00 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-256 86,-256 86,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;X -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M23.75,-215.89C21.95,-205.54 19.91,-192.06 19,-180 17.8,-164.04 17.8,-159.96 19,-144 19.64,-135.52 20.84,-126.34 22.12,-118.04\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"25.58,-118.55 23.75,-108.11 18.68,-117.41 25.58,-118.55\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M33.64,-216.41C36.91,-208.22 40.94,-198.14 44.62,-188.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"47.95,-190.05 48.41,-179.47 41.45,-187.45 47.95,-190.05\"/>\n",
       "</g>\n",
       "<!-- S -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>S</title>\n",
       "<ellipse fill=\"none\" stroke=\"red\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">S</text>\n",
       "</g>\n",
       "<!-- X&#45;&gt;S -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>X&#45;&gt;S</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27,-71.7C27,-63.98 27,-54.71 27,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-46.1 27,-36.1 23.5,-46.1 30.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- Y&#45;&gt;X -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>Y&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M48.36,-144.41C45.09,-136.22 41.06,-126.14 37.38,-116.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"40.55,-115.45 33.59,-107.47 34.05,-118.05 40.55,-115.45\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f8ce5fdbe20>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"T\", \"X\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"Y\", \"X\")\n",
    "g.edge(\"X\", \"S\")\n",
    "g.node(\"S\", \"S\", color=\"red\")\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A similar thing happens when we condition on a mediator of the treatment. A mediator is a variable between the treatment and the outcome. It, well, mediates the causal effect. For example, suppose again you can randomize education. But, just to be sure, you decide to control whether or not the person had a white-collar job. Once again, this conditioning biasses the causal effect estimation. This time, not because it opens a front door path with a collider, but because it closes one of the channels through which the treatment operates. In our example, getting a white-collar job is one way more education leads to higher pay. By controlling it, we close this channel and leave open only the direct effect of education on wages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.49.1 (20211004.0028)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"266pt\" height=\"188pt\"\n",
       " viewBox=\"0.00 0.00 266.10 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 262.1,-184 262.1,4 -4,4\"/>\n",
       "<!-- T -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>T</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">T</text>\n",
       "</g>\n",
       "<!-- X -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>X</title>\n",
       "<ellipse fill=\"none\" stroke=\"red\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">X</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;X -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>T&#45;&gt;X</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M48.36,-144.41C45.09,-136.22 41.06,-126.14 37.38,-116.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"40.55,-115.45 33.59,-107.47 34.05,-118.05 40.55,-115.45\"/>\n",
       "</g>\n",
       "<!-- Y -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>Y</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"55\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"55\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y</text>\n",
       "</g>\n",
       "<!-- T&#45;&gt;Y -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>T&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M58.25,-143.89C60.05,-133.54 62.09,-120.06 63,-108 64.2,-92.04 64.2,-87.96 63,-72 62.36,-63.52 61.16,-54.34 59.88,-46.04\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.32,-45.41 58.25,-36.11 56.42,-46.55 63.32,-45.41\"/>\n",
       "</g>\n",
       "<!-- X&#45;&gt;Y -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>X&#45;&gt;Y</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M33.64,-72.41C36.91,-64.22 40.94,-54.14 44.62,-44.95\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"47.95,-46.05 48.41,-35.47 41.45,-43.45 47.95,-46.05\"/>\n",
       "</g>\n",
       "<!-- educ -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>educ</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"223\" cy=\"-162\" rx=\"32.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"223\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">educ</text>\n",
       "</g>\n",
       "<!-- white collar -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>white collar</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"176\" cy=\"-90\" rx=\"65.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"176\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">white collar</text>\n",
       "</g>\n",
       "<!-- educ&#45;&gt;white collar -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>educ&#45;&gt;white collar</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M212.1,-144.76C206.45,-136.35 199.43,-125.89 193.08,-116.44\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"195.98,-114.48 187.5,-108.12 190.17,-118.38 195.98,-114.48\"/>\n",
       "</g>\n",
       "<!-- wage -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>wage</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"223\" cy=\"-18\" rx=\"35.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"223\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">wage</text>\n",
       "</g>\n",
       "<!-- educ&#45;&gt;wage -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>educ&#45;&gt;wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M234.31,-144.66C240.58,-134.59 247.74,-121.13 251,-108 254.85,-92.47 254.85,-87.53 251,-72 248.61,-62.36 244.11,-52.54 239.4,-44.01\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"242.39,-42.18 234.31,-35.34 236.35,-45.73 242.39,-42.18\"/>\n",
       "</g>\n",
       "<!-- white collar&#45;&gt;wage -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>white collar&#45;&gt;wage</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M187.38,-72.05C193.06,-63.59 200.04,-53.19 206.32,-43.84\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"209.38,-45.56 212.05,-35.31 203.57,-41.66 209.38,-45.56\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f8c6e345520>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gr.Digraph()\n",
    "g.edge(\"T\", \"X\")\n",
    "g.edge(\"T\", \"Y\")\n",
    "g.edge(\"X\", \"Y\")\n",
    "g.node(\"X\", \"X\", color=\"red\")\n",
    "\n",
    "g.edge(\"educ\", \"white collar\")\n",
    "g.edge(\"educ\", \"wage\")\n",
    "g.edge(\"white collar\", \"wage\")\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "source": [
    "To give a potential outcome argument, we know that, due to randomisation, the bias is zero $E[Y_0|T=0] - E[Y_0|T=1] = 0$. However, if we condition on the white-collar individuals, we have that $E[Y_0|T=0, WC=1] > E[Y_0|T=1, WC=1]$. That is because those who manage to get a white-collar job even without education are probably more hard-working than those who require the help of education to get the same job. With the same reasoning, $E[Y_0|T=0, WC=0] > E[Y_0|T=1, WC=0]$ because those that didn't get a white-collar job even with an education are probably less hard-working than those that didn't, but also didn't have any education. \n",
    "\n",
    "In our case, conditioning on the mediator induces a negative bias. It makes the effect of education seem lower than it actually is. This is the case because the causal effect is positive. If the effect were negative, conditioning on a mediator would have a positive bias. In all cases, this sort of conditioning makes the effect look weaker than it is. \n",
    "\n",
    "To put it more prosaic way, suppose that you have to choose between two candidates for a job at your company. Both have equally impressive professional achievements, but one does not have a higher education degree. Which one should you choose? Of course, you should go with the one without the higher education because he managed to achieve the same things as the other one but had the odds stacked against him.\n",
    "\n",
    "![image.png](./data/img/causal-graph/three_bias.png)\n",
    "\n",
    "## Key Ideas\n",
    "\n",
    "We've studied graphical models as a language to better understand and express causality ideas. We did a quick summary of the rules of conditional independence on a graph. This helped us then explore three structures that can lead to bias.\n",
    "\n",
    "The first was confounding, which happens when treatment and outcome have a common cause that we don't account for or control for. The second is selection bias due to conditioning on a common effect. The third structure is also a form of selection bias, this time due to excessive controlling of mediator variables. This excessive controlling could lead to bias even if the treatment was randomly assigned. Selection bias can often be fixed by simply doing nothing, which is why it is dangerous. Since we are biased toward action, we tend to see ideas that control things as clever when they can be doing more harm than good. \n",
    "\n",
    "## References\n",
    "\n",
    "I like to think of this entire book as a tribute to Joshua Angrist, Alberto Abadie and Christopher Walters for their amazing Econometrics class. Most of the ideas here are taken from their classes at the American Economic Association. Watching them is what is keeping me sane during this tough year of 2020.\n",
    "* [Cross-Section Econometrics](https://www.aeaweb.org/conference/cont-ed/2017-webcasts)\n",
    "* [Mastering Mostly Harmless Econometrics](https://www.aeaweb.org/conference/cont-ed/2020-webcasts)\n",
    "\n",
    "I'll also like to reference the amazing books from Angrist. They have shown me that Econometrics, or 'Metrics as they call it, is not only extremely useful but also profoundly fun.\n",
    "\n",
    "* [Mostly Harmless Econometrics](https://www.mostlyharmlesseconometrics.com/)\n",
    "* [Mastering 'Metrics](https://www.masteringmetrics.com/)\n",
    "\n",
    "My final reference is Miguel Hernan and Jamie Robins' book. It has been my trustworthy companion in the most thorny causal questions I had to answer.\n",
    "\n",
    "* [Causal Inference Book](https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/)\n",
    "\n",
    "![img](./data/img/poetry.png)\n",
    "\n",
    "## Contribute\n",
    "\n",
    "Causal Inference for the Brave and True is an open-source material on causal inference, the statistics of science. Its goal is to be accessible monetarily and intellectually. It uses only free software based on Python.\n",
    "If you found this book valuable and want to support it, please go to [Patreon](https://www.patreon.com/causal_inference_for_the_brave_and_true). If you are not ready to contribute financially, you can also help by fixing typos, suggesting edits, or giving feedback on passages you didn't understand. Go to the book's repository and [open an issue](https://github.com/matheusfacure/python-causality-handbook/issues). Finally, if you liked this content, please share it with others who might find it helpful and give it a [star on GitHub](https://github.com/matheusfacure/python-causality-handbook/stargazers)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
